﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestWebApplication.CommerceTransaction;
using TestWebApplication.CommerceTestDataProvider;
using Microsoft.Commerce.Proxy.TransactionService.v201001;
using TestWebApplication.CommercePaymentInstrument;
using CommercePIProxy = Microsoft.Commerce.Proxy.PaymentInstrumentService;
using CommerceAccountProxy = Microsoft.Commerce.Proxy.AccountService;
using System.Drawing;

namespace TestWebApplication
{
    public partial class mainpage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CommerceAccountProxy.Identity requester = (CommerceAccountProxy.Identity)Session["requester"];
            if (requester != null)
            {
                string username = requester.IdentityProperty[0].Value;
                test.Text = username + ", Welcome to the MINT API Test Web Application!";
            }

            loadSingleItems();
            loadBundles();

        }

        

        protected void choose_Click(object sender, CommandEventArgs e)
        {
            string itemid = (string)e.CommandArgument;
            PurchaseContentProductItemInput productItem = (PurchaseContentProductItemInput)ItemHandler.getItem(itemid);
            PurchaseContentViewModel purchaseInfo = new PurchaseContentViewModel();
            purchaseInfo.ContentCatalog.ContentCategory = "Application";
            purchaseInfo.ContentCatalog.LicenseType = "Full";
            purchaseInfo.ContentCatalog.EntitlementType = "Durable";
            purchaseInfo.ContentCatalog.Quantity = 1;
            purchaseInfo.ContentCatalog.contentName = productItem.Title;
            purchaseInfo.ContentCatalog.contentAmount = productItem.RevenueInfo.RevenueAllocationAmount;
            purchaseInfo.PIType = "Credit Card";
            Session["purchaseInfo"] = purchaseInfo;
            Response.Redirect("Purchase.aspx?itemID=" + itemid);
            /*
            string itemid = (string)e.CommandArgument;

            PurchaseInfoInput purchaseInfo = new PurchaseInfoInput();
            object item = ItemHandler.getItem(itemid);
            if (itemid.StartsWith("5"))
            {
                PurchaseBundleInput bundle = (PurchaseBundleInput)item;
                purchaseInfo = PurchaseHandler.createPurchaseInfo(bundle);
            }

            else if (itemid.StartsWith("8"))//subscriptions
            {
            }

            else
            {
                PurchaseContentProductItemInput productItem = (PurchaseContentProductItemInput)item;
                PurchaseBundleInput bundle = PurchaseHandler.wrapItem(productItem);
                purchaseInfo = PurchaseHandler.createPurchaseInfo(bundle);
            }

            Session["purchaseInfo"] = purchaseInfo;
            Response.Redirect("Purchase.aspx?itemID=" + itemid);*/

        }

        protected void loadSingleItems()
        {
            List<PurchaseContentProductItemInput> items = ItemHandler.loadAllSingleItems();

            if (items != null)
            {
                foreach (PurchaseContentProductItemInput item in items)
                {
                    TableRow row = new TableRow();
                    singleItems.Rows.Add(row);
                    TableCell cell = new TableCell();
                    cell.RowSpan = 5;

                    System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                    image.ImageUrl = "Resources\\image.jpg";
                    image.Width = 180;
                    cell.Controls.Add(image);
                    cell.Width = Unit.Percentage(20);
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    cell.Text = "Title:";
                    cell.Width = Unit.Percentage(40);
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    cell.Text = item.Title;
                    cell.Width = Unit.Percentage(40);
                    row.Cells.Add(cell);

                    row = new TableRow();
                    singleItems.Rows.Add(row);
                    cell = new TableCell();
                    cell.Text = "Type:";
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    cell.Text = item.ProductType;
                    row.Cells.Add(cell);

                    row = new TableRow();
                    singleItems.Rows.Add(row);
                    cell = new TableCell();
                    cell.Text = "Currency:";
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    cell.Text = "USD";
                    row.Cells.Add(cell);

                    row = new TableRow();
                    singleItems.Rows.Add(row);
                    cell = new TableCell();
                    cell.Text = "Amount:";
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    cell.Text = item.RevenueInfo.RevenueAllocationAmount.ToString();
                    row.Cells.Add(cell);

                    row = new TableRow();
                    singleItems.Rows.Add(row);
                    cell = new TableCell();
                    cell.Text = "Operation:";
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    Button button = new Button();
                    row.Cells.Add(cell);
                    button.CommandArgument = "100001";
                    button.BackColor = Color.Gainsboro;
                    button.BorderColor = Color.HotPink;
                    button.BorderStyle = BorderStyle.Solid;
                    button.BorderWidth = 1;
                    button.Text = "Choose It!!!";
                    button.Width = 80;
                    button.Height = 40;
                    button.Command += new CommandEventHandler(this.choose_Click);
                    cell.Controls.Add(button);
                    row.Cells.Add(cell);

                    row = new TableRow();
                    singleItems.Rows.Add(row);
                    cell = new TableCell();
                    cell.ColumnSpan = 3;
                    cell.Height = 100;
                    cell.Text = "Description:" + item.Description;
                    row.Cells.Add(cell);

                }
            }
        }

        protected void loadBundles()
        {
            List<PurchaseBundleInput> bundles = ItemHandler.loadAllBundles();

            if (bundles != null)
            {
                foreach (PurchaseBundleInput bundle in bundles)
                {
                    TableRow row = new TableRow();
                    bundleTable.Rows.Add(row);

                    TableCell cell = new TableCell();
                    cell.Text = "500001";
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    cell.Text = bundle.Title;
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    cell.Text = bundle.Amount.ToString();
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    cell.Text = bundle.Currency;
                    row.Cells.Add(cell);
                    cell = new TableCell();
                    cell.Text = bundle.Description;
                    row.Cells.Add(cell);

                    cell = new TableCell();
                    Button button = new Button();
                    row.Cells.Add(cell);
                    button.CommandArgument = "500001";
                    button.BackColor = Color.Gainsboro;
                    button.BorderColor = Color.HotPink;
                    button.BorderStyle = BorderStyle.Solid;
                    button.BorderWidth = 1;
                    button.Text = "Choose It!!!";
                    button.Width = 80;
                    button.Height = 40;
                    button.Command += new CommandEventHandler(this.choose_Click);
                    cell.Controls.Add(button);
                    row.Cells.Add(cell);

                    row = new TableRow();
                    bundleTable.Rows.Add(row);
                    cell = new TableCell();
                    cell.ColumnSpan = 6;
                    row.Cells.Add(cell);
                    Table table = new Table();
                    table.GridLines = GridLines.Both;
                    table.HorizontalAlign = HorizontalAlign.Center;
                    table.CellPadding = 15;
                    table.Width = Unit.Percentage(95);
                    cell.Controls.Add(table);

                    foreach (PurchaseContentProductItemInput item in bundle.PurchaseProductItemInputSet)
                    {
                        row = new TableRow();
                        table.Rows.Add(row);
                        cell = new TableCell();
                        cell.Width = Unit.Percentage(20);
                        cell.RowSpan = 4;
                        System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
                        image.ImageUrl = "Resources\\image.jpg";
                        image.Width = 180;
                        cell.Controls.Add(image);
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = "Title:";
                        cell.Width = Unit.Percentage(40);
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = item.Title;
                        cell.Width = Unit.Percentage(40);
                        row.Cells.Add(cell);

                        row = new TableRow();
                        table.Rows.Add(row);
                        cell = new TableCell();
                        cell.Text = "Type:";
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = item.ProductType;
                        row.Cells.Add(cell);

                        row = new TableRow();
                        table.Rows.Add(row);
                        cell = new TableCell();
                        cell.Text = "Currency:";
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = "USD";
                        row.Cells.Add(cell);

                        row = new TableRow();
                        table.Rows.Add(row);
                        cell = new TableCell();
                        cell.Text = "Amount:";
                        row.Cells.Add(cell);
                        cell = new TableCell();
                        cell.Text = item.RevenueInfo.RevenueAllocationAmount.ToString();
                        row.Cells.Add(cell);

                        row = new TableRow();
                        table.Rows.Add(row);
                        cell = new TableCell();
                        cell.ColumnSpan = 3;
                        cell.Height = 100;
                        cell.Text = "Description:" + item.Description;
                        row.Cells.Add(cell);
                    }

                }
            }
        }
    }
}